// 增加按钮
$("#b11").on('click',function(){
    var num=document.getElementById('num1').value;
    var username=document.getElementById('username1').value;
    var sex=document.getElementById('sex1').value;
    var pwd=document.getElementById('pwd1').value;
    var age=document.getElementById('age1').value;
    var birth=document.getElementById('birth1').value;
    var newli='<tr class="warning"><td><input type="checkbox"\
      name="item"/></td><td>'+num+'</td><td>'+username+'</td><td>'+sex+'</td>\
    <td>'+birth+'</td><td>'+pwd+'</td><td>'+age+'</td></tr>';
    if(num == null||username == null|| sex==null||birth==null||pwd==null||age==null){
        alert("插入的数据为空，请重新输入");
    }else {
        $("tbody").append( newli );
    }
})

// 修改按钮
$("#b33").on('click',function(){

    var num=document.getElementById('num2').value;
    var username=document.getElementById('username2').value;
    var sex=document.getElementById('sex2').value;
    var pwd=document.getElementById('pwd2').value;
    var age=document.getElementById('age2').value;
    var birth=document.getElementById('birth2').value;
    var newli='<tr class="warning"><td><input type="checkbox"\
      name="item"/></td><td>'+num+'</td><td>'+username+'</td><td>'+sex+'</td>\
    <td>'+birth+'</td><td>'+pwd+'</td><td>'+age+'</td></tr>';
    // $("tbody").append( newli );

    var olistTable = document.getElementById('listTable');
    var items = document.getElementsByName("item");
    for(var j=0;j<items.length;j++){    
        if(items[j].checked)//如果item被选中
        {
            var oParentnode = items[j].parentNode.parentNode;
            console.log(j);
            $("tr:eq("+(j+1)+")").after(newli);
            olistTable.removeChild(oParentnode);
        }
    }
})

// 删除按钮
$("#b44").on('click',function(){
    var num=document.getElementById('num3').value;
    var table = document.getElementById("listTable");
    var tr = table.getElementsByTagName("tr");

    // 循环表格每一行，查找匹配项
    for (i = 0; i < tr.length; i++) {
        td1 = tr[i].getElementsByTagName("td")[1];

        if ((td1.innerHTML.indexOf(num) > -1)) {
            tr[i].parentNode.removeChild(tr[i]);
        }
    }
})

// 查找按钮
$("#b4").on('click',function(){
    // 声明变量
    var inputNum,inputNum, table, tr, td1,td2, i;
    inputNum = document.getElementById("quary1").value;
    inputName = document.getElementById("quary2").value;
    table = document.getElementById("listTable");
    tr = table.getElementsByTagName("tr");
    // 循环表格每一行，查找匹配项
    for (i = 0; i < tr.length; i++) {
      td1 = tr[i].getElementsByTagName("td")[1];
      td2 = tr[i].getElementsByTagName("td")[2];
     
      if (inputNum) {
        if ((td1.innerHTML.indexOf(inputNum) > -1)) {
          tr[i].style.display = "";
        }
         else {
          tr[i].style.display = "none";
        }
      } 

      if (inputName) {
          console.log("td2 is open: "+td2);
        if ((td2.innerHTML.indexOf(inputName) > -1)) {
          tr[i].style.display = "";
        }
         else {
          tr[i].style.display = "none";
        }
      } 

    }
})

// 返回按钮
$("#b5").on('click',function(){
    // 声明变量
    var input, table, tr, td, i;
    input = document.getElementById("quary1").value;
    table = document.getElementById("listTable");
    tr = table.getElementsByTagName("tr");
    // 循环表格每一行，查找匹配项
    for (i = 0; i < tr.length; i++) {
          tr[i].style.display = "";
        }
})